<template>
  <el-dialog v-model="dialogVisible" title="创建活动" width="30%" @close="handleDialogClose">
    <el-form
      ref="formRef"
      :inline="true"
      style="margin-left: 25px"
      :rules="rules"
      :model="putaddlist"
      @submit.prevent="onSubmit"
    >
      <el-row :gutter="5">
        <el-col :span="12">
          <el-form-item label="渠道来源:" prop="channel">
            <el-select placeholder="" v-model="putaddlist.channel">
              <el-option v-for="item in channellist" :key="item.id" :value="item.content">{{
                item.content
              }}</el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="活动简介:" prop="introduction">
            <el-input placeholder="请输入10个字符以内" v-model="putaddlist.introduction">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="5">
        <el-col :span="12">
          <el-form-item label="活动类型:" prop="type">
            <el-select placeholder="" v-model="putaddlist.type">
              <el-option v-for="item in activitytype" :key="item.id" :value="item.content">{{
                item.content
              }}</el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="课程折扣:" prop="discount">
            <el-input placeholder="请输入" v-model="putaddlist.discount"> </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="5">
        <el-col :span="12">
          <el-form-item label="课程代金卷:" prop="voucher">
            <el-input placeholder="请输入" v-model="putaddlist.voucher"> </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开始时间:" prop="beginTime">
            <el-date-picker type="date" placeholder="请选择" v-model="putaddlist.beginTime" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="5">
        <el-col :span="12">
          <el-form-item label="结束时间" prop="endTime">
            <el-date-picker type="date" placeholder="请选择" v-model="putaddlist.endTime" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-button class="btncl" @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="btnOK" class="btnokstyle">确定</el-button>
    </el-form>
  </el-dialog>
</template>
<script setup>
import { ElMessage } from 'element-plus'
import { ref, onMounted } from 'vue'
import { putactivityList } from '@/api/activity'
import { cxTabelCourse } from '@/api/course'
const putaddlist = ref({
  type: '',
  discount: '',
  channel: '',
  introduction: '',
  beginTime: '',
  endTime: '',
  voucher: ''
})
onMounted(() => {
  tyoelist()
  channel()
})
const emit = defineEmits(['add-addList'])
const rules = ref({
  type: [
    {
      required: true,
      message: '请选择活动类型！',
      trigger: 'blur'
    }
  ],
  discount: [
    {
      required: true,
      message: '请输入课程折扣！',
      trigger: 'blur'
    },
    {
      pattern: /^(0\.[0-9]*[1-9]|[0]*\.[0-9]*[1-9][0-9]*)$/,
      message: '请输入大于0小于等于1的数字,最多保留两位小数！',
      trigger: 'blur'
    }
  ],
  channel: [
    {
      required: true,
      message: '请选择渠道来源！',
      trigger: 'blur'
    }
  ],
  introduction: [
    {
      required: true,
      message: '请输入活动简介！',
      trigger: 'blur'
    }
  ],
  beginTime: [
    {
      required: true,
      message: '请输入开始时间！',
      trigger: 'blur'
    }
  ],
  endTime: [
    {
      required: true,
      message: '请输入结束时间！',
      trigger: 'blur'
    }
  ],
  voucher: [
    {
      required: true,
      message: '请输入课程代金卷！',
      trigger: 'blur'
    },
    {
      pattern: /^(12[0-7]|1[0-1]\d|[1-9]?\d)$/,
      message: '请输入小于128的整数!',
      trigger: 'blur'
    }
  ]
})
const dialogVisible = ref(false)
const dialogStatus = () => {
  dialogVisible.value = true
}
const handleDialogClose = () => {
  formRef.value.resetFields()
  putaddlist.value = {
    name: '',
    applicablePerson: '',
    subject: '',
    price: '',
    info: '',
    id: ''
  }
}
const formRef = ref(null)
const btnOK = async () => {
  await formRef.value.validate()
  await putactivityList(putaddlist.value)
  await emit('add-addList')
  ElMessage({
    message: '添加课程成功！',
    type: 'success'
  })
  dialogVisible.value = false
}
//活动类型
const activitytype = ref([])
const tyoelist = async () => {
  const { data } = await cxTabelCourse(15)
  activitytype.value = data
}
const channellist = ref([])
const channel = async () => {
  const { data } = await cxTabelCourse(16)
  channellist.value = data
}
defineExpose({ dialogStatus, channellist })
</script>
<style scoped>
.el-form-item {
  display: block;
}
.btnokstyle {
  width: 94px;
  height: 40px;
  border-radius: 20px;
  background-color: #00cd95;
  color: #fff;
}
.btncl {
  width: 94px;
  height: 40px;
  border-radius: 20px;
  background-color: #eaeaea;
  color: #333;
  margin-left: 190px;
}
</style>
